import socket
import sys
import os
# 获取的地址
BASE_PATH = os.path.dirname(os.path.dirname(__file__))
sys.path.append(BASE_PATH)
from conf import settings


phone = socket.socket()
phone.connect(("127.0.0.1", 8080))

while 1:
    from_server = phone.recv(1024)
    print(from_server.decode("utf-8"))

    num = input("请输入(q或者Q关闭！)>>>")
    if not num:
        print("输入内容不为空！")
        continue
    elif num.upper() == "Q":
        print("通信关闭！")
        break
    phone.send(num.encode("utf-8"))
    if num == "1" or num == "2":
        name = input("请输入你的用户名")
        pwd = input("请输入你的密码")
        phone.send(f"{name}|{pwd}".encode("utf-8"))
        state_flag = phone.recv(1024).decode("utf-8")
        print(state_flag)
    elif num == "3" or num == "4":
        try:
            if state_flag == "登录成功！":
                if num == "3":
                    print("您的文件：")
                    for i in range(len(os.listdir(settings.UPLOAD_FILE_OUT))):
                        print(f"{i+1}:{os.listdir(settings.UPLOAD_FILE_OUT)[i]}")

                    while True:
                        try:
                            file_out = input("请输入你想上传的文件序号:").replace(" ", "")
                            if file_out.upper() == "Q":
                                break
                            elif os.listdir(settings.UPLOAD_FILE_OUT)[int(file_out)-1] in os.listdir(settings.UPLOAD_FILE_IN):
                                print("已上传，请重新输入！")
                                continue
                            file_true = os.path.join(settings.UPLOAD_FILE_OUT, os.listdir(settings.UPLOAD_FILE_OUT)[int(file_out)-1])
                            phone.send(os.listdir(settings.UPLOAD_FILE_OUT)[int(file_out)-1].encode("utf-8"))
                            with open(file_true, "rb")as f:
                                while 1:
                                    type_b = f.read(1024)
                                    if not type_b:
                                        break
                                    phone.send(type_b)
                            print(phone.recv(1024).decode("utf-8"))
                            break

                        except Exception:
                            print("输入错误！")
                            continue
                elif num == "4":
                    print("你可以下载的文件：")
                    for i in range(len(os.listdir(settings.UPLOAD_FILE_IN))):
                        print(f"{i + 1}:{os.listdir(settings.UPLOAD_FILE_IN)[i]}")
                    while True:
                        try:
                            speak_file = input("请输入你想下载的文件序号:").replace(" ", "")
                            if os.listdir(settings.UPLOAD_FILE_IN)[int(speak_file) - 1] in os.listdir(settings.UPLOAD_FILE_OUT):
                                print("已下载，请重新输入！")
                                continue
                            phone.send(os.listdir(settings.UPLOAD_FILE_IN)[int(speak_file) - 1].encode("utf-8"))
                            break
                        except Exception:
                            print("输入错误！")
                            continue
                    print(phone.recv(1024).decode("utf-8"))
        except Exception:
            print("未登录，请去登录！")
            continue
phone.close()
